package leetcode.L169;

import java.util.HashMap;
import java.util.Map;

class Solution {
    public int majorityElement(int[] nums) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
        }
        return majorityNum(map);
    }

    private Integer majorityNum(HashMap<Integer, Integer> map) {
        Map.Entry<Integer, Integer> entry1 = null;
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (entry1 == null || entry1.getValue() < entry.getValue()) {
                entry1 = entry;
            }
        }
        return entry1.getKey();
    }
}